// marc.cpp : implementation of the CMyMarc class
//

#include "stdafx.h"
#include "afx.h"

#include "MainFrm.h"
#include "Markup.h"

#include "ISISMARCDoc.h"
#include "ISISMARCView.h"
#include "MyIsisClient.h"
#include "WaitServer.h"

#include "global.h"

#include "isis.h"
#include "marc.h"

#include "conversion.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



/////////////////////////////////////////////////////////////////////////////
// CMyMarc

CMyMarc::CMyMarc()
{
}



//carga datos
CMyMarc::Iniciar(CString archivo)
{	

	
	
	CWaitServer * pdlg;

	pdlg = new CWaitServer();
	pdlg->SetLegend("IsisMarc is retrieving FDT21 from server");
    int success = pdlg->Create(IDD_DIALOGWSERVER);
	pdlg->ShowWindow(SW_SHOW);
	pdlg->UpdateWindow();
	
	
	Marc.RemoveAll();

	mie.AssignExternalDatabaseName(archivo+"[fdt21]");		
	mie.IsisIniciarRegistro(archivo);


	char area [MAXMFRL];
	int j;
	bool yacargue = false;
	cantreg = 0;

	//modificado #1
	int max = mie.IsisMaxRegistros();
	mie.IsisRegistrosFDT21();    

		//formar el xml general
    //hasta aca #1

	for (int i=1; i <= max; i++)
	{

		
		//if (mie.IsisRegistro(i,false,false) == 0)
        if (mie.IsisRegistroTemp(i) == 0) //metodo vero diana gaby
		{	
			marc mar;
			mar.visible=true;	//como no hay hoja de formatos elegida todos son visibles

			//campo 001
			    
			long r = mie.IsisCampo(1,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			CString lead = "LEADER";
			mar.c001_tag = area;

			//campo 002
			r = mie.IsisCampo(2,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			mar.c002_name = area;
			mar.titulo = area;

			//campo 005
			r = mie.IsisCampo(5,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			mar.c005_rep = atoi(area);

			//campo 006
			r = mie.IsisCampo(6,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			mar.c006_sub = atoi(area);

			//campo 015
			mar.cant015 = mie.IsisCantCampos(15);
			for (j = 1; j <= mar.cant015; j++)
			{
				r = mie.IsisSubCampo(15,j,"n",area, sizeof(area));
				//ConvertirToWin(area,area);
				CString sarea = area;
				if (j == 1 && sarea == "?")
				{
					j = mar.cant015 + 1;
					mar.cant015	= 0;
				}
				else
				{
					int pos_ = sarea.Find("-");
					int primero, ultimo;
					if (pos_ > 0)
					{
						primero = atoi(sarea.Left(pos_ + 1));
						ultimo = atoi(sarea.Right(sarea.GetLength() - primero - 2));
					}
					else
					{
						primero = atoi(sarea);
						ultimo = atoi(sarea);
					}
					mar.cant015 = mar.cant015 + ultimo - primero;
					CString cn = area;

					r = mie.IsisSubCampo(15,j,"t",area, sizeof(area));
					for (int jj = primero; jj <= ultimo; jj++)
						if (area[0] != 0)
						{
							if (pos_ > 0)
								mar.c015_ind1[j-1].c015_num.Format("%d", jj);
							else
								mar.c015_ind1[j-1].c015_num = cn;
							//ConvertirToWin(area,area);
							mar.c015_ind1[j - 1].c015_text = area;
						}
				}
			}

			//campo 016
			mar.cant016 = mie.IsisCantCampos(16);
			for (j = 1; j <= mar.cant016; j++)
			{
				r = mie.IsisSubCampo(16,j,"n",area, sizeof(area));
				//ConvertirToWin(area,area);
				CString sarea = area;
				if (j == 1 && sarea == "?")
				{
					j = mar.cant016 + 1;
					mar.cant016	= 0;
				}
				else
				{
					int pos_ = sarea.Find("-");
					int primero, ultimo;
					if (pos_ > 0)
					{
						primero = atoi(sarea.Left(pos_ + 1));
						ultimo = atoi(sarea.Right(sarea.GetLength() - primero - 2));
					}
					else
					{
						primero = atoi(sarea);
						ultimo = atoi(sarea);
					}
					mar.cant016 = mar.cant016 + ultimo - primero;
					CString cn = area;
					r = mie.IsisSubCampo(16,j,"t",area, sizeof(area));
					for (int jj = primero; jj <= ultimo; jj++)
						if (area[0] != 0)
						{
							if (pos_ > 0)
								mar.c016_ind2[j-1].c016_num.Format("%d", jj);
							else
								mar.c016_ind2[j-1].c016_num = cn;
							//ConvertirToWin(area,area);
							mar.c016_ind2[j - 1].c016_text = area;
						}
				}
			}

			//campo 020
			r = mie.IsisCampo(20,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			mar.c020_len = atoi(area);
			if (mar.c020_len != 0)
			{
				//campo 004
				r = mie.IsisCampo(4,i,1,area,MAXMFRL);
				//ConvertirToWin(area,area);
				mar.c004_type = area;
			}

			//campo 041
			r = mie.IsisCampo(41,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			mar.c041_ghelp = area;

			//campo 055
			r = mie.IsisCampo(55,i,1,area,MAXMFRL);
			//ConvertirToWin(area,area);
			mar.c055_valid = area;

			//campo 100
			int oc = mie.IsisCantCampos(100);
			mar.cant100 = mie.IsisCantCampos(100);
			for (j = 1; j <= mar.cant100; j++)
			{
				int ordinal = 0;
				//subcampo o
				r = mie.IsisSubCampo(100,j,"o",area, sizeof(area));;
				//ConvertirToWin(area,area);
				if (atoi(area) != 0)
					ordinal = atoi(area) - 1;
				mar.c100_subf[ordinal].c100o_ord = atoi(area);
				if (mar.c020_len == 0)
				{
					//subcampo c
					r = mie.IsisSubCampo(100,j,"c",area, sizeof(area));
					//ConvertirToWin(area,area);
					mar.c100_subf[ordinal].c100c_subf = area;
				}

				//subcampo n
				r = mie.IsisSubCampo(100,j,"n",area, sizeof(area));
				
				//ConvertirToWin(area,area);
				//AfxMessageBox(area);
				mar.c100_subf[ordinal].c100n_name = area;
				//subcampo r
				r = mie.IsisSubCampo(100,j,"r",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100r_rep = atoi(area);
				//subcampo l
				r = mie.IsisSubCampo(100,j,"l",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100l_len = atoi(area);
				//subcampo d
				r = mie.IsisSubCampo(100,j,"d",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100d_def = area;
				//subcampo w
				r = mie.IsisSubCampo(100,j,"w",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100w_entry = atoi(area);
				//subcampo x
				r = mie.IsisSubCampo(100,j,"x",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100x_mst = area;
				//subcampo k
				r = mie.IsisSubCampo(100,j,"k",area, sizeof(area));;
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100k_key = area;
				//subcampo f
				r = mie.IsisSubCampo(100,j,"f",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100f_format = area;
				//subcampo u
				r = mie.IsisSubCampo(100,j,"u",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100u_format = area;			
				//subcampo a
				r = mie.IsisSubCampo(100,j,"a",area, sizeof(area));
				//ConvertirToWin(area,area);
				mar.c100_subf[ordinal].c100a_atr = area;
			}
			if (mar.c020_len != 0 )
				yacargue = true;
			bool encontre = false;
			int ii = 0;
			int size = Marc.GetSize();
			if (size > 0 && mar.c001_tag >= Marc[size - 1].c001_tag)
				ii = Marc.GetSize();
			else
				if (size > 0 && mar.c001_tag >= Marc[0].c001_tag)
				{
					while (!encontre && ii < size)
					{
						if (Marc[ii].c001_tag >= mar.c001_tag)
							encontre = true;
						else ii++;
					}
				}
			Marc.InsertAt(ii,mar);
			cantreg++;

		}
	}

	pdlg->DestroyWindow();
	delete (pdlg);
	pdlg = NULL;
	
	
		
};


CMyMarc::ArchAyuda(Codigos codigos[10], CString archivo, int lead)
{
	int jj = 0;
	

	CWaitServer * pdlg;

	//cws = new CWaitServer();
	//cws->ShowWindow(SW_SHOW);
	//cws->UpdateWindow();
	
	pdlg = new CWaitServer();
	pdlg->SetLegend("IsisMarc is retrieving Leader data");
    int success = pdlg->Create(IDD_DIALOGWSERVER);
	pdlg->ShowWindow(SW_SHOW);
	pdlg->UpdateWindow();
	
	
	//mie.AssignExternalDatabaseName(archivo+"[fdt21]");		
	//mie.IsisIniciarRegistro(archivo);
	char area [MAXMFRL];


	//int max = mie.IsisMaxRegistros();
	int max = mie.IsisMaxRegistrosTemp();// metodo creado por diana vero gaby

	for (int i=1; i <= max; i++)
	{
	
	if (mie.IsisRegistroTemp(i) == 0) //metodo vero diana gaby
		{	
	

			//IsisRecLeader leader;
			//IsisRecLeaderMap(ms, 0, TOCHAR(leader));

			long r = mie.IsisCampo(1,i,1,area, sizeof(area));

			//r = IsisRecField(ms, 0, 1, 1, area, sizeof(area));
			ConvertirToWin(area,area);
			CString ctag = area;
			if (atoi(ctag) >= lead && atoi(ctag)<=lead+18)
			{

				CString sub_w, sub_x, sub_k, sub_f, sub_a;

				
				//r = IsisRecSubField(ms, 0, 100, 1, "w", area, sizeof(area));

				r = mie.IsisSubCampo(100,1,"w",area, sizeof(area));
				
				//ConvertirToWin(area,area);
				sub_w = area;


				//r = IsisRecSubField(ms, 0, 100, 1, "x", area, sizeof(area));
				//ConvertirToWin(area,area);

				r = mie.IsisSubCampo(100,1,"x",area, sizeof(area));				
				
				sub_x = area;

				r = mie.IsisSubCampo(100,1,"k",area, sizeof(area));
				
				//r = IsisRecSubField(ms, 0, 100, 1, "k", area, sizeof(area));
				//ConvertirToWin(area,area);
				sub_k = area;


				r = mie.IsisSubCampo(100,1,"f",area, sizeof(area));
				
				//r = IsisRecSubField(ms, 0, 100, 1, "f", area, sizeof(area));
				//ConvertirToWin(area,area);
				sub_f = area;


				r = mie.IsisSubCampo(100,1,"a",area, sizeof(area));
				
				//r = IsisRecSubField(ms, 0, 100, 1, "a", area, sizeof(area));
				//ConvertirToWin(area,area);
				sub_a = area;

				codigos[jj].cod = sub_x;
				codigos[jj].idioma = sub_f.GetLength()>0?sub_f[1]:' ';
				codigos[jj++].clave = sub_k;

			}
		}

	
	}

    pdlg->DestroyWindow();
	delete (pdlg);
	pdlg = NULL;
	
};



CMyMarc:: ~CMyMarc()
{
}
